﻿using System.Windows;
using DSPL.Services;
using DSPL.Contracts;
using DSPL.Common;
using System.Data;
using DSPL.Services.Services;
using DSPL.Contracts.Common;
using DSPL.Contracts.ViewModel;
using DSPL.Contracts.Command;
using DSPL.Finac.Common;
using System;
using System.Collections.Generic;
using DSPL.Contracts.Interfaces;
using DSPL.Common.Classes;

namespace DSPL.Finac.ViewModel
{
    public class InterLocStockTransferViewModel : BaseViewModel
    {
        private bool isReferenceNoFocused;        
        private bool isLocationFromCodeFocused;
        private bool isLocationToCodeFocused;
        private bool isCostCodeFocused;
        private string fromLocationCode;
        private string toLocationCode;
        private string fromLocationName;
        private string toLocationName;

        #region Define properties


        public bool IsLocationFromCodeFocused
        {
            get { return isLocationFromCodeFocused; }
            set
            {
                isLocationFromCodeFocused = value;
                this.OnPropertyChanged("IsLocationCodeFocused");

            }
        }

        public bool IsLocationToCodeFocused
        {
            get { return isLocationToCodeFocused; }
            set
            {
                isLocationToCodeFocused = value;
                this.OnPropertyChanged("IsLocationToCodeFocused");

            }
        }

        public bool IsCostCodeFocused
        {
            get { return isCostCodeFocused; }
            set
            {
                isCostCodeFocused = value;
                this.OnPropertyChanged("IsCostCodeFocused");

            }
        }

        
        public bool IsReferenceNoFocused
        {
            get { return isReferenceNoFocused; }
            set
            {
                isReferenceNoFocused = value;
                this.OnPropertyChanged("IsReferenceNoFocused");
            }
        }


        public string FromLocationCode
        {
            get
            {
                return fromLocationCode;
            }
            set
            {
                fromLocationCode = value;
                this.OnPropertyChanged("FromLocationCode");
            }
        }
        
        public string ToLocationCode
        {
            get
            {
                return toLocationCode;
            }
            set
            {
                toLocationCode = value;
                this.OnPropertyChanged("ToLocationCode");
            }
        }

        public string FromLocationName
        {
            get
            {
                return fromLocationName;
            }
            set
            {
                fromLocationName = value;
                this.OnPropertyChanged("FromLocationName");
            }
        }

        public string ToLocationName
        {
            get
            {
                return toLocationName;
            }
            set
            {
                toLocationName = value;
                this.OnPropertyChanged("ToLocationName");
            }
        }


        #endregion


        #region Constructor

        public InterLocStockTransferViewModel()
            : base()
        {
            MessageTitle = "Inter Location Stock Transfer";
        }

        #endregion
        /// <summary>
        /// Validate mandatory data before save
        /// </summary>
        /// <param name="obj"></param>
        protected override void PreDataValidateExecuted(object obj)
        {
           
            IsLocationFromCodeFocused = false;
            IsCostCodeFocused = false;            
            IsReferenceNoFocused = false;

            var messageService = new MessageService();
            if (CurrentRow["RefNo"].ToString().Trim() == string.Empty)
            {
                messageService.ShowMessage(MessageText.ReferenceNoNotEmpty, MessageTitle, MessageType.InfoOK);
                IsReferenceNoFocused = true;
                this.IsValid = false;
                return;
            }
                     

            if (CurrentRow["LocationPk"] == DBNull.Value || Convert.ToInt32(CurrentRow["LocationPk"]) == 0)
            {
                messageService.ShowMessage(MessageText.LocationNotEmpty, MessageTitle, MessageType.InfoOK);
                IsLocationFromCodeFocused = true;
                this.IsValid = false;
                return;
            }

            if (CurrentRow["CostPk"] == DBNull.Value || Convert.ToInt32(CurrentRow["CostPk"]) == 0)
            {
                messageService.ShowMessage(MessageText.CostCodeNotEmpty, MessageTitle, MessageType.InfoOK);
                IsCostCodeFocused = true;
                this.IsValid = false;
                return;
            }


            this.IsValid = true;
        }

        protected override void RefreshExternalDataObjectsExecueted(object obj)
        {

            base.RefreshExternalDataObjectsExecueted(obj);
            AddColumnsToChildTable();

        }

        private void AddColumnsToChildTable()
        {
            if (DtChildTransaction != null)
            {
                if (!DtChildTransaction.Columns.Contains("ItemCode"))
                {
                    DtChildTransaction.Columns.Add("ItemCode");
                }
                if (!DtChildTransaction.Columns.Contains("ItemName"))
                {
                    DtChildTransaction.Columns.Add("ItemName");
                }
                if (!DtChildTransaction.Columns.Contains("Qoh"))
                {
                    DtChildTransaction.Columns.Add("Qoh");
                }
            }
        }

        protected override void AddAddionalReportParametersExecuted(object obj)
        {
            base.AddAddionalReportParametersExecuted(obj);

            if (AdditionalReportParameters == null)
            {
                AdditionalReportParameters = new List<IReportParameters>();
            }
            FromLocationCode = "xx";
            ToLocationCode = "xx";
            FromLocationName = "xx";
            ToLocationName = "xxx";
            AdditionalReportParameters.Add(new ReportParameters() { ParameterIndex = 8, ParameterName = "FromLocationCode", InputValue = FromLocationCode});
            AdditionalReportParameters.Add(new ReportParameters() { ParameterIndex = 9, ParameterName = "ToLocationCode", InputValue = ToLocationCode });
            AdditionalReportParameters.Add(new ReportParameters() { ParameterIndex = 10, ParameterName = "ToLocationName", InputValue = ToLocationName });
            AdditionalReportParameters.Add(new ReportParameters() { ParameterIndex = 11, ParameterName = "FromLocationName", InputValue = FromLocationName });
        }

    }
}


